package com.ys.controller.web.base;

import com.ys.config.MenuId;
import com.ys.config.MyConfig;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.base.TbPayrollSlip;
import com.ys.entity.base.TbPayrollSlipItem;
import com.ys.entity.base.TbUserPayApply;
import com.ys.service.base.TbPayrollSlipService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/web/base/tbPayrollSlip")
public class WebTbPayrollSlipController {


	@Autowired
	private CommonApiService<TbPayrollSlip> commonApiService;

	@Autowired
	private TbPayrollSlipService tbPayrollSlipService;
	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		try {
			String bid = commonApiService.getCommonNextBid(MenuId.TB_PayrollSlip);
			TbPayrollSlip tbPayrollSlip = new TbPayrollSlip()
					.setStatus(1)
					.setBid(bid)
					.setCreateTime(new Date());

			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TB_PayrollSlip, new ArrayList<Map<String, Object>>());
			String toAdd = commonApiService.toAdd(model, tbPayrollSlip, MenuId.TB_PayrollSlip, null, subGridConfig);
			if (!"test".equals(MyConfig.getActive()) && !"pro".equals(MyConfig.getActive())) {
				return toAdd;
			}else {
				return "base/tbPayrollSlip/tbPayrollSlipAdd";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}

	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		try {
			if (params.get("bid") == null){
				return W.error(model, "单号不能为空");
			}

			String bid = (params.get("bid").toString());

			Map<String, Object> entity = tbPayrollSlipService.getExtraParam(bid);
			if (entity == null){
				return W.error(model, "工资付款单不存在!");
			}

			List<Map<String, Object>> sonlist = tbPayrollSlipService.getItemList(bid);

			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TB_PayrollSlip, sonlist);

			String toEdit = commonApiService.toEdit(model, Long.valueOf(entity.get("id").toString()), new TbPayrollSlip(), MenuId.TB_PayrollSlip, entity, subGridConfig);
			if (!"test".equals(MyConfig.getActive()) && !"pro".equals(MyConfig.getActive())) {
				return toEdit;
			}else {
				return "base/tbPayrollSlip/tbPayrollSlipEdit";
			}
		} catch (Exception e) {
			return e.getMessage();
		}
	}

}
